package com.zbf.stock.listener;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.zbf.stock.mapper.TestJIanHuoMapper;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2021/3/12  15:25
 * 描述:
 */
@Component
@RabbitListener(queues = {"MY-QUEUE"})
public class MyQeueListeer {


    @Autowired
    private TestJIanHuoMapper testJIanHuoMapper;


    @RabbitHandler
    public void orderForJianHuoHandler(String json, Message message, Channel channel) throws IOException {

        System.out.println("==========处理订单数据==========");
        //订单信息   id  goodsId  quantity
        Map<String,Object> map = JSON.parseObject(json, Map.class);

        map.put("orderId",map.get("id"));

        try {
            //防止重复的查询
            List<Map<String, Object>> orderList = testJIanHuoMapper.getOrder(map);
            if(orderList.size()==0){
                //插入单据表
                testJIanHuoMapper.insertJianHUoOrder(map);
            }

        }catch (Exception e){

            //将出错的数据加入定时任务表

        }finally {
            //获取消息的标识
            long deliveryTag = message.getMessageProperties().getDeliveryTag();
            channel.basicAck(deliveryTag,true);
        }

    }

}
